package cn.net.lwj.sjqy.service.Impl;

import cn.net.lwj.sjqy.base.BaseDAO;
import cn.net.lwj.sjqy.dao.mysql.SjqyConfigDao;
import cn.net.lwj.sjqy.entity.JsonResult;
import cn.net.lwj.sjqy.entity.SjqyConfig;
import cn.net.lwj.sjqy.service.SjqyService;
import java.util.List;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.SpringUtil;

@Log
@Service
public class SjqyServiceImpl implements SjqyService {
    @Autowired
    private SjqyConfigDao sjqyConfigDao;
    @Override
    @SuppressWarnings("all")
    @Transactional(rollbackFor = Exception.class)
    public JsonResult sjqy() {
        log.info("5555555555");
        List<SjqyConfig> sjqyConfigs = sjqyConfigDao.selectAll();
        for (SjqyConfig config : sjqyConfigs) {
            String daoName = config.getDaoName();
            String tableName = config.getTableName();
            Integer qybz = config.getQybz();
            if (qybz == null || qybz == 0) continue;
            try {
                BaseDAO sourceDao = (BaseDAO)SpringUtil.getBean(Class.forName("cn.net.lwj.sjqy.dao.mysql." + daoName));
                BaseDAO targetDao = (BaseDAO) SpringUtil.getBean(Class.forName("cn.net.lwj.sjqy.dao.mysql1." + daoName + "1"));;
                List list = sourceDao.selectAll();
                System.out.println(list);
                sjqy(list, targetDao);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return JsonResult.responseSuccess("操作成功!");
    }

    @SuppressWarnings("all")
    @Transactional(rollbackFor = Exception.class)
    public void sjqy(List list, BaseDAO targetDao) throws Exception{
        list.forEach(u -> {
            log.info(u.toString());
            targetDao.delete(u);
            targetDao.insert(u);
        });
    }

}
